package delegate;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import model.DataManagerModel;

/**
 * Class - CreateRecipeViewController
 * Description - Application window for creating a recipe
 * Notes - First iteration for Lunchbox GUI, keeping it simple using the MVC design concept
 * 
 * @author Ryne Okimoto
 * 
 * Inspired by Fred Swartz December 2004 
 * from Ohio State University CSE 421 Winter 2009
 */

@SuppressWarnings("serial")
public class CreateRecipeViewController extends JFrame
{
	// Class data
	private static String DEFAULT_RECIPE_NAME  = "<Enter recipe name>";
	private static String DEFAULT_CATEGORY     = "<Enter category>";
	private static String DEFAULT_TAGS         = "<Enter tags>";
	private static String DEFAULT_INGREDIENTS  = "<Enter ingredients>";
	private static String DEFAULT_INSTRUCTIONS = "<Enter instructions>";
	
	private DataManagerModel dataModel;
	private JButton          btnDone        = new JButton("Done");
	private JButton          btnCancel      = new JButton("Cancel");
	private JTextField       txtRecipeName  = new JTextField(DEFAULT_RECIPE_NAME, 30);
	private JTextField       txtCategory    = new JTextField(DEFAULT_CATEGORY, 15);
	private JTextField       txtTags        = new JTextField(DEFAULT_TAGS, 30);
	private JTextField       txtImage       = new JTextField(20);
	private JTextArea        txaIngredients = new JTextArea(DEFAULT_INGREDIENTS, 10, 30);
	private JTextArea        txaRecipeInput = new JTextArea(DEFAULT_INSTRUCTIONS, 10, 30);
	
	/**
	 * Constructor
	 * Description - Instantiates the view using the data model
	 * 
	 * @param model
	 */
	public CreateRecipeViewController(DataManagerModel model)
	{
		// Set model for viewController
		dataModel = model;
		
		// Set layout and properties
		JPanel view = new JPanel();
		view.setLayout(new GridBagLayout());
		this.setResizable(false);
		
		btnDone.setFocusable(true);
		btnCancel.setFocusable(true);
		txtRecipeName.setFocusable(true);
		txtCategory.setFocusable(true);
		txtTags.setFocusable(true);
		
		txaIngredients.setWrapStyleWord(true);
		txaIngredients.setLineWrap(true);
		txaIngredients.setFocusable(true);
		
		txaRecipeInput.setWrapStyleWord(true);
		txaRecipeInput.setLineWrap(true);
		txaRecipeInput.setFocusable(true);
		
		// Set GridBagConstraints
		GridBagConstraints gbcTxaRecipeInput = new GridBagConstraints();
		GridBagConstraints gbcTxtRecipeName  = new GridBagConstraints();
		GridBagConstraints gbcBtnDone        = new GridBagConstraints();
		GridBagConstraints gbcTxtCategory    = new GridBagConstraints();
		GridBagConstraints gbcTxaIngredients = new GridBagConstraints();
		GridBagConstraints gbcTxtTags        = new GridBagConstraints();
		GridBagConstraints gbcBtnCancel      = new GridBagConstraints();
		
		// Constraints - instructions
		gbcTxaRecipeInput.gridx = 0;
		gbcTxaRecipeInput.gridy = 4;
		gbcTxaRecipeInput.fill = GridBagConstraints.HORIZONTAL;
		gbcTxaRecipeInput.insets = new Insets(10, 10, 10, 10);
		gbcTxaRecipeInput.gridwidth = 3;
		
		// Constraints - recipe name
		gbcTxtRecipeName.gridx = 0;
		gbcTxtRecipeName.gridy = 0;
		gbcTxtRecipeName.insets = new Insets(10, 10, 0, 10);
		
		// Constraints - category
		gbcTxtCategory.gridx = 0;
		gbcTxtCategory.gridy = 1;
		gbcTxtCategory.insets = new Insets(10, 10, 0, 10);
		
		// Constraints - tags
		gbcTxtTags.gridx = 0;
		gbcTxtTags.gridy = 2;
		gbcTxtTags.insets = new Insets(10, 10, 0, 10);
		
		// Constraints - ingredients
		gbcTxaIngredients.gridx = 0;
		gbcTxaIngredients.gridy = 3;
		gbcTxaIngredients.fill = GridBagConstraints.HORIZONTAL;
		gbcTxaIngredients.insets = new Insets(10, 10, 0, 10);
		gbcTxaIngredients.gridwidth = 3;
		
		// Constraints - done button
		gbcBtnDone.gridx = 0;
		gbcBtnDone.gridy = 5;
		gbcBtnDone.insets = new Insets(0, 10, 0, 10);
		
		// Constraints - cancel button
		gbcBtnCancel.gridx = 0;
		gbcBtnCancel.gridy = 6;
		gbcBtnDone.insets = new Insets(10, 10, 10, 10);
		
		// Add components
		view.add(txtRecipeName, gbcTxtRecipeName);
		view.add(txtCategory, gbcTxtCategory);
		view.add(txaRecipeInput, gbcTxaRecipeInput);
		view.add(txaIngredients, gbcTxaIngredients);
		view.add(btnDone, gbcBtnDone);
		view.add(txtTags, gbcTxtTags);
		view.add(btnCancel, gbcBtnCancel);
		
		// Register listeners
		btnDone.addActionListener(new DoneClickedListener());
		btnCancel.addActionListener(new CancelClickedListener());
		txtRecipeName.addFocusListener(new TextFocusListener());
		txtCategory.addFocusListener(new TextFocusListener());
		txtTags.addFocusListener(new TextFocusListener());
		txaIngredients.addFocusListener(new TextFocusListener());
		txaRecipeInput.addFocusListener(new TextFocusListener());
		
		this.setContentPane(view);
		this.setTitle("Create Recipe");
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.pack();
	} // Constructor
	
	/**
	 * Method - validateInputFields
	 * Description - 
	 * @return
	 */
	private boolean validateInputFields(){
		boolean isInputOK = true;
		StringBuilder errorMessage = new StringBuilder("Please enter information for:\n");
		
		if(txtRecipeName.getText().equalsIgnoreCase(DEFAULT_RECIPE_NAME)){
			errorMessage.append("recipe name\n");
			isInputOK = false;
		}
		
		if(txtCategory.getText().equalsIgnoreCase(DEFAULT_CATEGORY)){
			errorMessage.append("category\n");
			isInputOK = false;
		}
		
		if(txtTags.getText().equalsIgnoreCase(DEFAULT_TAGS)){
			errorMessage.append("tags\n");
			isInputOK = false;
		}
		
		if(txaIngredients.getText().equalsIgnoreCase(DEFAULT_INGREDIENTS)){
			errorMessage.append("ingredients\n");
			isInputOK = false;
		}
		
		if(txaRecipeInput.getText().equalsIgnoreCase(DEFAULT_INSTRUCTIONS)){
			errorMessage.append("instructions\n");
			isInputOK = false;
		}
			
		if(!isInputOK){
			JOptionPane.showMessageDialog(this, errorMessage.toString(), 
					"Incomplete fields", JOptionPane.ERROR_MESSAGE);
		}
		
		return isInputOK;
	}
	
	/**
	 * Class - CancelClickedListener
	 * Description - 
	 */
	private class CancelClickedListener implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			CreateRecipeViewController.this.dispose();
		}
	}
	
	/**
	 * Class - DoneClickedListener
	 * Description - Validates the field data and adds a filemanager
	 *               to the data model
	 */
	private class DoneClickedListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(validateInputFields()){
				if(dataModel.addFileManager
					(txtRecipeName.getText(), txtCategory.getText(), 
					txaIngredients.getText(), txaRecipeInput.getText(),
					txtTags.getText(), txtImage.getText())){
							
						JOptionPane.showMessageDialog(CreateRecipeViewController.this, "Recipe created!");
						CreateRecipeViewController.this.dispose();
				}
			}
		}
	}
	
	/**
	 * Class - TextFocusListener
	 * Description - Listens for focus changes on components
	 * @author Ryne
	 *
	 */
	private class TextFocusListener implements FocusListener{
		
		/**
		 * Method - focusGained
		 * Description - Clears text in component if it is default
		 */
		public void focusGained(FocusEvent e){
			ComponentEvent ce = new ComponentEvent(e.getComponent(), e.getID());
			Component temp = ce.getComponent();
			if(temp instanceof JTextField){
				JTextField tf = (JTextField) temp;
				if(
					tf.getText().equalsIgnoreCase(DEFAULT_RECIPE_NAME) ||
					tf.getText().equalsIgnoreCase(DEFAULT_TAGS) ||
					tf.getText().equalsIgnoreCase(DEFAULT_CATEGORY)){
					tf.setText("");
				}
				
			}
			else if(temp instanceof JTextArea){
				JTextArea ta = (JTextArea) temp;
				if(
					ta.getText().equalsIgnoreCase(DEFAULT_INSTRUCTIONS) ||
					ta.getText().equalsIgnoreCase(DEFAULT_INGREDIENTS)
				){
					ta.setText("");
				}
			}
			// For debugging to see which component is selected
			//System.out.println("Component: " + temp);
		}
		
		/**
		 * Method - focusLost
		 * Description - Resets text in component if nothing was entered
		 */
		public void focusLost(FocusEvent e){
			ComponentEvent ce = new ComponentEvent(e.getComponent(), e.getID());
			Component temp = ce.getComponent();
			
			if(temp instanceof JTextField){
				JTextField tf = (JTextField) temp;
				if(tf.equals(txtRecipeName) && tf.getText().isEmpty()){
					tf.setText(DEFAULT_RECIPE_NAME);
				}
				else if(tf.equals(txtCategory) && tf.getText().isEmpty()){
					tf.setText(DEFAULT_CATEGORY);
				}
				else if(tf.equals(txtTags) && tf.getText().isEmpty()){
					tf.setText(DEFAULT_TAGS);
				}	
			}	
			else if(temp instanceof JTextArea){
				JTextArea ta = (JTextArea) temp;
				if(ta.equals(txaIngredients) && ta.getText().isEmpty()){
					ta.setText(DEFAULT_INGREDIENTS);
				}
				else if(ta.equals(txaRecipeInput) && ta.getText().isEmpty()){
					ta.setText(DEFAULT_INSTRUCTIONS);
				}
			}
		}
	}
	
	/**
	 * Unit Tester
	 * @param args
	 */
	public static void main(String[] args)
	{
		DataManagerModel model = new DataManagerModel();
		CreateRecipeViewController view = new CreateRecipeViewController(model);
		view.setVisible(true);
		
	} // main
} // CreateRecipeViewController
